package com.xiaobao.coalproperty.module.find.view;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.google.gson.Gson;
import com.xiaobao.coalproperty.R;
import com.xiaobao.coalproperty.module.home.model.entity.BlankEntity;
import com.xiaobao.coalproperty.other.base.BaseActivity;
import com.xiaobao.coalproperty.other.constant.UrlConstant;
import com.xiaobao.coalproperty.other.util.HttpHelperUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.BindView;
import okhttp3.Call;

public class SendTopicActivity extends BaseActivity {

    @BindView(R.id.send_topic_title)
    EditText edtTitle;
    @BindView(R.id.send_topic_content)
    EditText edtContent;

    @Override
    protected int getContentView() {
        return R.layout.activity_send_topic;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        setLeftClickFinish();
        setTitle("发帖子");
    }

    public void onSendTopicClick(View view) {
        if (TextUtils.isEmpty(edtTitle.getText())) {
            showToast("标题不能为空");
            return;
        }
        if (TextUtils.isEmpty(edtContent.getText())) {
            showToast("内容不能为空");
            return;
        }
        addForum();
    }

    private void addForum() {
        OkHttpUtils.post().url(UrlConstant.URL_ADD_FORUM)
                .params(HttpHelperUtils.getInstance().addForumParams(edtTitle.getText().toString(), edtContent.getText().toString()))
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                showToast(e.getMessage());
            }

            @Override
            public void onResponse(String response, int id) {
                try {

                    BlankEntity entity = new Gson().fromJson(response, BlankEntity.class);
                    if (entity.getCode() == 0) {
                        showToast("发帖成功");
                        finish();
                        return;
                    }
                    showToast(entity.getMsg());
                } catch (Exception e) {
                }
            }
        });
    }
}
